<template>
  <a-modal :width="1000" v-model:visible="showDetailDialog" title="变更单详情" :footer="false" top="8vh" title-align="start" :mask-closable="false" @cancel="closedDialog">
    <div class="detail-w">
      <div class="header">
        <p class="title">基础信息</p>
      </div>
      <a-form ref="formRef" :model="formInfo">
        <a-row>
          <a-col :span="8">
            <a-form-item label="报价ID：" label-col-flex="100px">
              <p>{{ formInfo.billNo }}</p>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="变更类型：" label-col-flex="100px">
              <p>{{ formInfo.changeType === 'quote' ? '变更报价' : '变更产品' }}</p>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="变更方：" label-col-flex="100px">
              <p>{{ formInfo.createType === 'company' ? '经销商' : '供应商' }}</p>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="8">
            <a-form-item label="经销商：" label-col-flex="100px">
              <p>{{ formInfo.companyName }}</p>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="供应商：" label-col-flex="100px">
              <p>{{ formInfo.supplierName }}-{{ formInfo.supplierCode }}</p>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="提交人：" label-col-flex="100px">
              <p>{{ formInfo.createUname }}</p>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="8">
            <a-form-item label="销售员：" label-col-flex="100px">
              <p>{{ formInfo.createUname }}</p>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="采购员：" label-col-flex="100px">
              <p>{{ formInfo.purchaseUname }}</p>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="提交时间：" label-col-flex="100px">
              <p>{{ $setTime(formInfo.gmtCreate) }}</p>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="8">
            <a-form-item label="供应商审批：" label-col-flex="100px">
              <template v-if="formInfo.supplierAuditUname">
                <p>
                  <span>【{{ formInfo.supplierAuditUname }}】</span>
                  <span>{{ auditMap[formInfo.supplierAudit] }}，</span>
                  <span>{{ $setTime(formInfo.gmtSupplierAudit, 'YYYY/MM/DD HH:MM') }}</span>
                </p>
                <p>{{ formInfo.supplierAuditRemark }}</p>
              </template>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="经销商审批：" label-col-flex="100px">
              <template v-if="formInfo.companyAuditUname">
                <p>
                  <span>【{{ formInfo.companyAuditUname }}】</span>
                  <span>{{ auditMap[formInfo.companyAudit] }}，</span>
                  <span>{{ $setTime(formInfo.gmtCompanyAudit, 'YYYY/MM/DD HH:MM') }}</span>
                </p>
                <p>{{ formInfo.companyAuditRemark }}</p>
              </template>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="变更原因：" label-col-flex="100px">
              <p>{{ formInfo.remark }}</p>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div class="header" style="margin-top: 20px">
        <p class="title">变更内容</p>
      </div>
      <!-- 价格变更 -->
      <div class="price-w" v-if="formInfo.changeType == 'quote'">
        <ul>
          <template v-for="(val, key) in fieldMap" :key="key">
            <li class="flex change-item" v-if="formInfo['new' + key]">
              <p class="label">{{ val }}：</p>
              <div class="flex-c">
                <p style="text-align: right">
                  <span v-if="key == 'InvoiceType'">{{ invoiceTypeMap[formInfo['old' + key]] }}</span>
                  <span v-else-if="key == 'QuoteType'">{{ quoteTypeMap[formInfo['old' + key]] }}</span>
                  <span v-else>{{ formInfo['old' + key] }}</span>
                  <span v-if="key == 'ParTaxRate' || key == 'RefundTaxRate'">%</span>
                </p>
                <icon-arrow-right style="margin: 0 10px" />
                <p style="text-align: right">
                  <span v-if="key == 'InvoiceType'">{{ invoiceTypeMap[formInfo['new' + key]] }}</span>
                  <span v-else-if="key == 'QuoteType'">{{ quoteTypeMap[formInfo['new' + key]] }}</span>
                  <span v-else>{{ formInfo['new' + key] }}</span>
                  <span v-if="key == 'ParTaxRate' || key == 'RefundTaxRate'">%</span>
                </p>
              </div>
            </li>
          </template>
          <li class="flex change-item">
            <p class="label">含税单价：</p>
            <div class="flex">
              <a-table class="table-normal-line" :data="formInfo.quoteItems" size="mini" :bordered="false" :pagination="false">
                <template #columns>
                  <a-table-column title="数量" :width="100">
                    <template #cell="{ record }">
                      <span style="margin-right: 5px">>=</span>
                      <span>{{ record.quantity }}</span>
                    </template>
                  </a-table-column>
                  <a-table-column title="含税单价" :width="100">
                    <template #cell="{ record }">
                      <span style="margin-right: 5px">{{ currencySymbol[formInfo.currency] || '￥' }}</span>
                      <span>{{ record.taxPrice }}</span>
                    </template>
                  </a-table-column>
                  <a-table-column title="不含税单价" :width="100">
                    <template #cell="{ record }">
                      <span style="margin-right: 5px">{{ currencySymbol[formInfo.currency] || '￥' }}</span>
                      <span>{{ record.price }}</span>
                    </template>
                  </a-table-column>
                </template>
              </a-table>
              <icon-arrow-right style="margin: 0 10px" />
              <a-table class="table-normal-line" :data="formInfo.quoteChangeItems" size="mini" :bordered="false" :pagination="false">
                <template #columns>
                  <a-table-column title="数量" :width="100">
                    <template #cell="{ record }">
                      <span style="margin-right: 5px">>=</span>
                      <span>{{ record.quantity }}</span>
                    </template>
                  </a-table-column>
                  <a-table-column title="含税单价" :width="100">
                    <template #cell="{ record }">
                      <span style="margin-right: 5px">{{ currencySymbol[formInfo.currency] || '￥' }}</span>
                      <span>{{ record.taxPrice }}</span>
                    </template>
                  </a-table-column>
                  <a-table-column title="不含税单价" :width="100">
                    <template #cell="{ record }">
                      <span style="margin-right: 5px">{{ currencySymbol[formInfo.currency] || '￥' }}</span>
                      <span>{{ record.price }}</span>
                    </template>
                  </a-table-column>
                </template>
              </a-table>
            </div>
          </li>
        </ul>
      </div>
      <!-- 产品变更 -->
      <div class="product-w" v-if="formInfo.changeType == 'product'" style="margin-top: 10px">
        <p>新增报价产品：</p>
        <a-table class="table-two-line" style="flex: 1" ref="tableRef" row-key="sku" :data="formInfo.quoteChangeProducts" :scroll="{ y: '100%' }" size="mini" :bordered="false" :pagination="false">
          <template #columns>
            <a-table-column data-index="img" title="图片" :width="60">
              <template #cell="{ record }">
                <ImgPopover :src="record.productImg">
                  <template #con>
                    <a-image width="40" height="40" fit="contain" :src="record.productImg" />
                  </template>
                </ImgPopover>
              </template>
            </a-table-column>
            <a-table-column title="产品名称/类目" :width="200">
              <template #cell="{ record }">
                <TextLimit :rows="1">{{ record.productName }}</TextLimit>
                <p>{{ record.cateName || '--' }}</p>
              </template>
            </a-table-column>
            <a-table-column data-index="sku" title="经销商SKU" :width="120" />
            <a-table-column data-index="spu" title="经销商SPU" :width="120" />
            <a-table-column data-index="ssku" title="供应商SKU" :width="120" />
            <a-table-column data-index="sspu" title="供应商SPU" :width="120" />
          </template>
        </a-table>
      </div>
    </div>
  </a-modal>
  <!-- 选择采购产品 -->
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { getSupplierQuoteChangeInfo } from '@/api/supplier'
  import { currencySymbol } from '@/utils/globalData'

  onMounted(() => {
    getDetail()
  })
  const emit = defineEmits(['closed', 'success'])
  const props = defineProps({
    isShow: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
    },
  })

  const auditMap = {
    '0': '未审核',
    '1': '同意',
    '-1': '驳回',
  }
  const showDetailDialog = ref(props.isShow)
  const formInfo = ref<any>({})
  const formRef = ref()
  const invoiceTypeMap = {
    0: '不开票',
    1: '普票',
    2: '专票',
  }
  const quoteTypeMap = {
    1: '非梯度报价',
    2: '梯度报价',
  }
  const fieldMap = {
    Moq: '最小起订量',
    Currency: '币种',
    InvoiceType: '发票类型',
    ParTaxRate: '票面税率',
    RefundTaxRate: '退税率',
    DeliveryDays: '交期',
    QuoteType: '报价类型',
  }

  // 获取详情
  async function getDetail() {
    const res: any = await getSupplierQuoteChangeInfo({ id: props.id })
    formInfo.value = res?.data || {}
  }
  // 关闭
  function closedDialog() {
    emit('closed')
    showDetailDialog.value = false
  }
</script>
<style lang="less" scoped>
  .detail-w {
    height: calc(84vh - 100px);
    display: flex;
    flex-direction: column;
    overflow: auto;
  }
  .arco-row {
    margin-bottom: 0;
  }
  .label {
    width: 120px;
    text-align: right;
  }
  .product-w {
    flex: 1;
    display: flex;
    overflow: hidden;
  }
  .arco-form-item {
    margin-bottom: 0;
  }
  :deep(.arco-form-item-label-col) {
    padding-right: 0px;
  }
  .change-item {
    margin-top: 8px;
  }
  .header {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 10px;
    border-left: 3px solid rgba(var(--primary-6), 0.5);
    padding-left: 10px;
    .title {
      margin-right: 30px;
      width: 90px;
    }
  }
</style>
